<template>
    <el-dialog title="添加" :visible.sync="dialogFormVisible" :close-on-click-modal=this.$store.state.theme.interface.other.closeOnClickModal append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="small">
            <el-form-item label="父级分类" prop="name">
                <el-select v-model="form.parent_id" placeholder="选择分类" style="display: inline;">
                    <el-option label="顶级分类" :value="0"></el-option>
                    <el-option v-for="item in categoryData" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" placeholder="请输分类名称"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input v-model="form.sort" placeholder="请输排序值（从大到小）"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>
            <el-button size="mini" type="primary" @click="add()">确 定</el-button>
        </div>
    </el-dialog>
</template>
  
<script>
import { add, getParentCategory } from '@/api/renovation/category';
export default {
    name: "add",
    data() {
        return {
            categoryData: [],
            form: {
                parent_id: 0,
                name: '',
                sort: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' }
                ]
            },
            dialogFormVisible: false
        }
    },
    mounted() {
        this.initCategoryData();
    },
    methods: {
        initCategoryData() {
            getParentCategory().then(res => {
                this.categoryData = res.data
            })
        },
        add() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    add(JSON.parse(JSON.stringify(this.form))).then(res => {
                        this.dialogFormVisible = !this.dialogFormVisible;
                        this.$message({
                            message: res.msg,
                            type: 'success'
                        });
                        this.$emit('updateCategoryData');
                    })
                } else {
                    console.log('error submit!');
                    return false;
                }
            });
        }
    },
    watch: {
        'dialogFormVisible': {
            handler: function () {
                if (this.dialogFormVisible) {
                    this.initCategoryData();
                }
                this.$nextTick(() => {
                    this.$refs['form'].resetFields();
                });
            }
        }
    },
}
</script>
  
<style lang="less" scoped></style>